//
//  HMFeedbackController.m
//  仿真开发
//
//  Created by LZH on 2017/3/25.
//  Copyright © 2017年 B.HOPE. All rights reserved.
//

#import "HMFeedbackController.h"

@interface HMFeedbackController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *sugguestText;

@property (weak, nonatomic) IBOutlet UILabel *restTextLabel;

@end

@implementation HMFeedbackController

-(instancetype)init{
    if (self = [super init]) {
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"HMFeedbackView" bundle:nil];
        self = [story instantiateInitialViewController];
      
    }
    return self;
}


//UITextViewDelegate代理方法实现
//开始编辑
-(void)textViewDidBeginEditing:(UITextView *)textView{
    self.sugguestText.textColor = [UIColor blackColor];
    if ([self.sugguestText.text isEqualToString:@"请输入您宝贵的建议"]) {
        self.sugguestText.text = @"";
    }
}

//结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView {
    if (_sugguestText.text.length<1) {
        _sugguestText.text = @"请输入您宝贵的建议";
    }
}
//4.当textView发生改变的方法
-(void)textViewDidChange:(UITextView *)textView{
   
    NSInteger length = 30 - self.sugguestText.text.length;
    
    //设置提示用语
    self.restTextLabel.text = [NSString stringWithFormat:@"还能输入%zd个字",length];
    self.restTextLabel.textColor = length>=0 ?[UIColor blackColor] :[UIColor redColor];
    
    self.restTextLabel.text = length>=0 ?[NSString stringWithFormat:@"还能输入%zd个字",length] : [NSString stringWithFormat:@"已经超出%zd个字",length];
}



#pragma mark-初始化界面
-(void)setupUI{
    //1.设置textView
    self.sugguestText.text = @"请输入您宝贵的建议";
    self.sugguestText.textColor = [UIColor grayColor];
    //设置textView的代理
    self.sugguestText.delegate = self;
}


- (void)viewDidLoad{
    [super viewDidLoad];
    [self setupUI];
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    //输入完毕隐藏键盘
    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapGr.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGr];
    
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(clickRightItem)];
}

-(void)clickRightItem{
    NSInteger length = self.sugguestText.text.length;
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler: nil];
    
    if (length <=30 && length!=0&&![_sugguestText.text isEqualToString:@"请输入您宝贵的建议"]) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"正在进行中...." preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:action];
        
        [self presentViewController:alertController animated:YES completion:nil];
        //设置时间断点
        [self performSelector:@selector(disMiss:) withObject:alertController afterDelay:1.5];
        
    }else if (length == 0||
              [_sugguestText.text isEqualToString:@"请输入您宝贵的建议"]){
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"内容不能为空" preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:action];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else{
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"超出字数!" preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:action];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
    }
}

-(void)disMiss:(UIAlertController*)VC{
    [VC dismissViewControllerAnimated:YES completion:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];
}



//手势的实现
-(void)viewTapped:(UITapGestureRecognizer*)tapGr{
    [self.sugguestText
     
    resignFirstResponder];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}


@end
